package com.xhb.biz.service;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.solon.service.impl.ServiceImpl;
import com.xhb.biz.feign.UserDataSyncService;
import com.xhb.biz.mapper.GroupMapper;
import com.xhb.biz.po.GroupPO;
import org.noear.solon.aspect.annotation.Service;
import org.noear.solon.data.annotation.Tran;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Group)}表服务接口
 *
 * @author tanyukun
 * @since 2023-02-06 13:44:04
 */
@Service
public class GroupService extends ServiceImpl<GroupMapper, GroupPO> {

    /**
     * 首页导航分组同步
     *
     * @param userService feign 接口
     * @param headers     请求头
     */
    @Tran
    public void userNavigationGroupListSync(UserDataSyncService userService, Map<String, Object> headers) {
        String data = userService.getNavigationGroupList(headers);
        List<GroupPO> groupPos = JSONUtil.toList(data, GroupPO.class);
        if (CollectionUtils.isNotEmpty(groupPos)) {
            super.saveOrUpdateBatch(groupPos);
            // 删除已被管理端删除的旧数据
            LambdaQueryWrapper<GroupPO> delWrapper = new LambdaQueryWrapper<>();
            delWrapper.notIn(GroupPO::getId, groupPos.stream().map(GroupPO::getId).collect(Collectors.toList()));
            super.remove(delWrapper);
        }
    }


}
